<?php

namespace app\admin\model\flow;

use think\Config;
use think\Db;
use think\Model;
use app\common\constant;

class Cooperation extends Model
{
    // 表名
    protected $name = 'flow_cooperation';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'type_text'
    ];

    public function getTypeList()
    {
        return ['1' => __('Type 1'),'2' => __('Type 2'),'3' => __('Type 3'),'4' => __('Type 4')];
    }

    public function getTypeTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function department()
    {
        return $this->belongsTo('Department', 'department_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * 所有部门
     */
    public static function allDepartmentName()
    {
        return Db::name("department")->order('weigh desc')->where(['status' => 'normal'])->column('id,name');
    }

    /**
     * 获取前后多少天的到期合作
     */
    public static function getEndList($params)
    {
        $enddate = Config::get("site.enddate");

        //获取当前时间
        $before_dawn = time();
        $before_data = date('Y-m-d');

        $start_time = date('Y-m-d', strtotime('-'.$enddate.' day', $before_dawn));
        $end_time   = date('Y-m-d', strtotime('+'.$enddate.' day', $before_dawn));

        $uInfo = $params['uInfo'];
        //设置自己账号只能查看自己的
        $unitWhere = [];
        if(!empty($uInfo['id']) && isset($uInfo['id'])){
            $unitWhere['originator'] = ['in', $uInfo['id']];
        }

        //如果是管理员账号可以查看所有的
        if(in_array($uInfo['id'],constant\Consts::$Super)
            or $uInfo['nickname']=='王姝'
            or $uInfo['nickname']=='刘霆' or   $uInfo['nickname']=='张大松') {
            $unitWhere = [];
        }
        else if(!empty($uInfo['id']) && isset($uInfo['id'])){
            $unitWhere['originator'] = ['in', $uInfo['id']];
        }

        $endList = Db::name('cooperation')
            ->where('status',2)
            ->where($unitWhere)
            ->where('enddate', '>=' ,$start_time)
            ->where('enddate', '<=' ,$end_time)
            ->field('id,cname,enddate')
            ->select();

        $result = [];
        //对数据进行处理
        if(isset($endList[0])){
            $tmp = [];
            foreach ($endList as $endObj) {
                //计算当前日期和到期时间的天数
                $days = constant\Common::diff_date($before_data,$endObj['enddate']);
                $tmp[] = [
                    "Id"             => $endObj['id'],
                    "cname"          => $endObj['cname'],
                    "enddate"        => $endObj['enddate'],
                    "days"           => $days,
                ];
            }
            $result = $tmp;
            unset($tmp);
        }
        return $result;
    }
}
